package clock;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Timer;

import listener.ParticipantChangeEvent;
import listener.ParticipantChangeListener;

public class SecondTimer implements ActionListener, ParticipantChangeListener {
	private final static int SECOND = 1000;
	private Timer timer;
	private Second second;

	public SecondTimer() {
		timer = new Timer(SECOND, this);
	}

	public void start() {
		second.decrease();
		timer.start();
	}

	public void pause() {
		timer.stop();
	}

	public boolean isPaused() {
		return !timer.isRunning();
	}

	public void actionPerformed(ActionEvent e) {
		second.decrease(); 
	}

	public void participantChange(ParticipantChangeEvent e) {
		if (!isPaused())
			pause();
		this.second = e.getSource().getClock().getSecond();
	}
}
